<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Configuration
 *
 * @author MinhNguyen
 */
// Include twitteroauth
include 'Libs/facebook/facebookoauth.php';

class Facebook{
    //put your code here
    public $facebook;
    public static $instance;
    public $config = array(
        'appId' => '1407313896157096',
        'secret' => 'c75db84522a831e28fdeaafa3d894e4d',
      );
    
    public static function getInstance() {
        if (!self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    public function __get($name) {
        return $this->$name;
    }
    
    public function __set($name, $value) {
        $this->$name = $value;
    } 
    
    public function __construct() {
        //allocate your stuff
    }
    
    public static function withData() {
        $instance = self::getInstance();
        $instance->facebook = new FacebookOAuth($instance->config);
        return $instance; 
    }
    
    public static function getUserId(){
        $instance = self::getInstance();
        $instance->facebook = new FacebookOAuth($instance->config);
        $user_id = $instance->facebook->getUser();
        return $user_id;
    }   
    
    public static function getAuthorization() {
        $instance = self::withData();
        $db = Database::getInstance();
        $id = $db->getID("facebooks", "id");
        $userId = $_SESSION['userid'];
        $accesstoken = $instance->facebook->getAccessToken();
        $facebookid = $instance->facebook->getUser();
        $me = $instance->facebook->api('/me');
        $screenname = $me['username'];
        $query = "INSERT INTO facebooks (id, userid, accesstoken, facebookid,
            screenname) VALUES ('$id', '$userId', '$accesstoken', '$facebookid', '$screenname')";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            return "true";
        }
        else {
            return "false";
        }
    }
    
    public static function getLoginURL() {
        $instance = self::withData();
        $url = $instance->facebook->getLoginUrl( array( 'scope' => 'publish_stream' ));
        return $url;
    }   
    
    public static function postFacebookStatus($message, $link){
        if (isset($_SESSION['userid'])) {
            $db = Database::getInstance();
            $userId = $_SESSION['userid'];
            $query = "SELECT * FROM facebooks WHERE userid = '$userId'";
            $result = $db->query($query);
             if ($result->returnCode == 200) {
                if ($result->dataTable->numOfRow == 1) {
                    $rows = $result->dataTable->rows[0];
                    $instance = self::withData();
                    $response = $instance->facebook->api('/'.$rows['facebookid'].'/feed?access_token='.$rows['accesstoken'], 'post', array('message'=> $message, 'link' => $link));                  
                    if ($response != 200) {
                        //Do something if the request was unsuccessful
                        return "There was an error posting the message.";
                    }
                    return "Posted";
                }else {
                    return "Not found data";
                }
            }else {
                return "Cannot connect database";
            }
        }else {
            return "No user log in";
        }
    }
    
    public static function getFacebooks() {
        if (isset($_SESSION['userid'])) {
            $db = Database::getInstance();
            $userId = $_SESSION['userid'];
            $query = "SELECT * FROM facebooks WHERE userid = '$userId'";
            $result = $db->query($query);
             if ($result->returnCode == 200) {
                if ($result->dataTable->numOfRow > 0) {
                    foreach ($result->dataTable->rows as $row) {
                        $name = $row['screenname'];
                        $id = $row['facebookid'];
                        $userid = "$userid$id;";
                        $username = "$username$name;";
                    }
                    return array(
                        "screenname" => $username,
                        "id" => $userid
                    );
                }else {
                    return array(
                        "screenname" => 'undefined',
                        "id" => 'undefined'
                    );
                }
            }else {
                return array(
                    "screenname" => 'undefined',
                    "id" => 'undefined'
                );
            }
        }else {
            return array(
                "screenname" => 'undefined',
                "id" => 'undefined'
            );
        }
    }
    
    public static function deleteFacebook($facebookid) {
        if(isset($_SESSION['userid'])) {
            $db = Database::getInstance();
            $query = "DELETE FROM facebooks WHERE facebookid = '$facebookid'"; 
            $result = $db->query($query);
            if ($result->returnCode == 200) {
                return "true";
            }
            else {
                return "false";
            }
        }
        else {
            return "false";
        }
    }
}
?>
